import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

SubView0{
    id: root
    width: 252
    height: 325
    property int saveType:-1;
    function savePara(){
        if(saveType < 0)
        {
            return;
        }
        var result;
        switch(saveType)
        {
        case 0:
            if(mymodbus.weight<mymodbus.upperLimit&&mymodbus.weight>mymodbus.lowerLimit){
                result = mytables.setOKCount(mytables.okCount+1)
            }
            else
            {
                result = mytables.setNGCount(mytables.ngCount+1)
            }
            break;
        case 1:
            result = mytables.clearCount()
            break;
        }
        loaderText.visible=false;
        saveType=-1;
        if(!result)
        {
            countPanelDialog.open()
        }
    }
    Text {
        id: sum
        x: 22
        y: 25
        text: qsTr("总数")
        color: "white"
        font.pixelSize: 20
    }
    TextField
    {
        id: txtTotal
        x: 102
        y: 10
        width: 123
        height: 40
        text: mytables.okCount+mytables.ngCount
        color: "#F0D070"
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 20
        font.bold:true
        background: Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    border.width: 0
                    border.color: "#F0D070"
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "#F0D070"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: txtTotal.height
                            PathLine {
                                x: txtTotal.width
                                y: txtTotal.height
                            }
                        }
                    }
                }
    }
    Text {
        id: okCount
        x: 29
        y: 103
        text: "OK"
        color: "white"
        font.pixelSize: 25
    }
    TextField
    {
        id: txtOK
        x: 102
        y: 89
        width: 123
        height: 40
        text: mytables.okCount
        color: "#03B915"
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 20
        font.bold:true
        background: Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    border.width: 0
                    border.color: "#03B915"
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "#03B915"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: txtTotal.height
                            PathLine {
                                x: txtTotal.width
                                y: txtTotal.height
                            }
                        }
                    }
                }
    }
    Text {
        id: ngCount
        x: 29
        y: 183
        text: "NG"
        color: "white"
        font.pixelSize: 25
    }
    TextField
    {
        id: txtNG
        x: 102
        y: 170
        width: 123
        height: 40
        text: mytables.ngCount
        color: "#D9001B"
        horizontalAlignment: Text.AlignHCenter
        font.pixelSize: 20
        font.bold:true
        background: Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    border.width: 0
                    border.color: "#D9001B"
                    // 绘制一条边框
                    Shape {
                        anchors.fill: parent
                        ShapePath {
                            strokeWidth: 3
                            strokeColor: "#D9001B"
                            strokeStyle: ShapePath.SolidLine
                            startX: 0
                            startY: txtTotal.height
                            PathLine {
                                x: txtTotal.width
                                y: txtTotal.height
                            }
                        }
                    }
                }
    }
    Button
    {
        id:btnStart
        x: 76
        y: 239
        width: 110
        height: 36
        background: Rectangle{
            border.width: 0
            color: "#C070B0"
                }

        text: qsTr("开始")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {
            //timer.setTimeout(function()
            //{

            //}, 500);
            if(loaderText.visible)
            {
                return;
            }
            loaderText.visible=true;
            saveType=0;
            funcTimer.start();  //C  为什么延时100ms后执行？
        }
    }
    Button
    {
        id:btnSetZero
        x: 76
        y: 281
        width: 110
        height: 36
        background: Rectangle{
            border.width: 0
            color: "#7584C2"
                }

        text: qsTr("清除")
        font.pixelSize: 25
        palette.buttonText: "white"
        onClicked: {

            if(loaderText.visible)
            {
                return;
            }
            loaderText.visible=true;
            saveType=1;
            funcTimer.start();
        }
    }

    Timer {
        id:funcTimer
                running: false
                repeat: false
                interval:100
                onTriggered: {
                    savePara()  //哦哦，qml中的js。
                }
            }
    Timer {
        id: timer
        function setTimeout(cb, delayTime) {
            timer.interval = delayTime;
            timer.repeat = false;
            timer.triggered.connect(cb);
            timer.triggered.connect(function release () {
                timer.triggered.disconnect(cb); // This is important
                timer.triggered.disconnect(release); // This is important as well
            });
            timer.start();
        }
    }
}
